package com.briup.bookstore.service.impl;

import com.briup.bookstore.dto.AddressAddDTO;
import com.briup.bookstore.dto.AddressUpdateDTO;
import com.briup.bookstore.mapper.AddressMapper;
import com.briup.bookstore.mapper.UserMapper;
import com.briup.bookstore.po.ext.AddressExtend;
import com.briup.bookstore.service.AddressService;
import com.briup.bookstore.utils.BeanCopyUtils;
import com.briup.bookstore.vo.AddressVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author qinyc
 * @description 针对表【es_address】的数据库操作Service实现
 * @createDate 2023-07-18 21:33:00
 */
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<AddressVO> findAddresses(Integer userId) {
        List<AddressExtend> list = addressMapper.findByUserId(userId);
        List<AddressVO> vos = BeanCopyUtils.copyBeanList(list, AddressVO.class);
        return vos;
    }

    @Override
    public void save(AddressAddDTO addressAddDTO) {
        addressMapper.save(addressAddDTO);
    }

    @Override
    public void update(AddressUpdateDTO addressUpdateDTO) {
        addressMapper.update(addressUpdateDTO);
    }

    @Override
    public void setDefaultAddress(Integer userId, Integer addressId) {
        userMapper.setDefaultAddress(userId, addressId);
    }

    @Override
    public void cancelDefaultAddress(Integer addressId) {
        userMapper.cancelDefaultAddress(addressId);
    }

    @Override
    public void delete(Integer id) {
        userMapper.cancelDefaultAddress(id);
        addressMapper.deleteById(id);
    }

}




